function ModelUtils() {
};

ModelUtils.get = function(task, models, states, transitions) {
  var m, s, t = [];
  
  for (var i = 0, il = models.length; i < il; i++) {
    var tmp = models[i];
    if (tmp.id == task.model_id) {
      m = tmp;
    }
  }
  if (!m) {
    return null;
  }
  for (var i = 0, il = states.length; i < il; i++) {
    var tmp = states[i];
    if (tmp.id == task.state_id && tmp.model_id == m.id) {
      s = tmp;
    }
  }
  if (!s) {
    return null;
  }
  for (var i = 0, il = transitions.length; i < il; i++) {
    var tmp = transitions[i];
    if (tmp.source_id == s.id && tmp.model_id == m.id) {
      t[t.length] = tmp;
    }
  }
  return { m: m, s: s, t: t };
};

ModelUtils.evt = function(task, state, transition, user, date) {
  var e = pz_task_event.instance();
  e.task_id = task.id;
  e.date = date;
  e.user_id = user.id;;
  e.state_id = state.id;
  e.transition_id = transition.id;
  return e;
};

ModelUtils.trk = function(task, transition) {
  var t = MiscUtils.clone(task);
  delete t.id;
  t.task_id = task.id;
  t.transition_id = transition.id;
  return t;
};

ModelUtils.btns = function(task, models, states, transitions, div, user, date, callbackFunc, callbackFunc1) {
  if (task.is_canceled == 1) {
    return;
  }
  
  var div1 = document.createElement('span');
  div.appendChild(div1);
  div2 = document.createElement('span');
  div.appendChild(div2);
  
  var r = ModelUtils.get(task, models, states, transitions);
  if (r && r.t) {
    for (var i = 0, il = r.t.length; i < il; i++) {
      var tmp = r.t[i];
      if (user.type != tmp.user_type) {
        if (task.model_id != 3) {
          DOMUtils.removeChildElements(div2);;
          if (tmp.user_type == Constant.USER_TYPE_DESIGNER) {
            div2.appendChild(document.createTextNode(task.designer));
          } else {
            div2.appendChild(document.createTextNode(tmp.user_type));
          }
          continue;
        } else {
          if (user.id != task.custom_assigner_id  && (r.s.id == 19 || r.s.id == 21)) {
            DOMUtils.removeChildElements(div2);
            div2.style.margin = (user.type == Constant.USER_TYPE_ADMINISTER) ? '0 0 0 7px' : '0px';
            div2.appendChild(document.createTextNode(task.custom_assigner));
            continue;
          }
          if (user.id != task.custom_assignee_id && (r.s.id == 18 || r.s.id == 20)) {
            DOMUtils.removeChildElements(div2);
            div2.style.margin = (user.type == Constant.USER_TYPE_ADMINISTER) ? '0 0 0 7px' : '0px';
            div2.appendChild(document.createTextNode(task.custom_assignee));
            continue;
          }
        }
      }
      btn = document.createElement('input');
      btn.type = 'button';
      btn.className = 'button';
      btn.value = tmp.name;
      btn._state = r.s;
      btn._transition = tmp;
      btn._task = task;
      btn.onclick = function() { this.disabled = true;
                                 if (parseInt((this._task.deadline - date) / 86400, 10) <= 0) {
                                   var popupBox = new ModulePopupBox();
                                   var div = document.createElement('div');
                                   var task = this._task;
                                   var transition = this._transition;
                                   var pos = DOMUtils.findPos(this);
                                   var func1 = function() { if (task._text == '') {
                                                              window.alert('不能为空');
                                                              return;
                                                            }
                                                            ModelUtils.op(transition, task, r, user, date, callbackFunc, callbackFunc1, pos, {overdue: task._text});
                                                            popupBox._close();
                                                          };
                                   var func2 = function() { popupBox._close();
                                                            callbackFunc();
                                                          };
                                   MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: 'OverDue Reason', width: 500, height: 50 });
                                   new ModuleDialogInput(document, div, null, null, user, date, { object: task, dfault: ''});
                                 } else {
                                   ModelUtils.op(this._transition, this._task, r, user, date, callbackFunc, callbackFunc1, DOMUtils.findPos(this), null);
                                 }
                               };
      div1.appendChild(btn);
    }
  }
};


ModelUtils.btns1 = function(task, models, states, transitions, div, user, date, callbackFunc) {
  if (task.is_canceled == 1) {
    return;
  }
  var popupBox = new ModulePopupBox();
  var div1 = document.createElement('span');
  div.appendChild(div1);
  div2 = document.createElement('span');
  div.appendChild(div2);
  div3 = document.createElement('span');
  div.appendChild(div3);
  div4 = document.createElement('span');
  div.appendChild(div4);
  
  var r = ModelUtils.get(task, models, states, transitions);
  if (r && r.t) {
    if (r.t.length == 0) {
      div1.appendChild(document.createTextNode((PPublishUtils.object(task.state_id, states)) ? PPublishUtils.object(task.state_id, states).name : ''));
      div1.appendChild(document.createTextNode(', '));
      
      div2.appendChild(document.createTextNode((task.deadline != 0) ? parseInt((task.deadline - date) / 86400, 10) + ' Days left, ' : '-, '));
    }
    
    for (var i = 0, il = r.t.length; i < il; i++) {
      var tmp = r.t[i];
      DOMUtils.removeChildElements(div1);
      DOMUtils.removeChildElements(div2);
      DOMUtils.removeChildElements(div3);
      DOMUtils.removeChildElements(div4);
      
      if (user.type != tmp.user_type) {
        if (tmp.user_type == Constant.USER_TYPE_DESIGNER) {
          div1.appendChild(document.createTextNode(task.designer));
        } else {
          div1.appendChild(document.createTextNode(tmp.user_type));
        }
        div1.appendChild(document.createTextNode(' - '));
      
        div2.appendChild(document.createTextNode((PPublishUtils.object(task.state_id, states)) ? PPublishUtils.object(task.state_id, states).name : ''));
        div2.appendChild(document.createTextNode(', '));
      
        div3.appendChild(document.createTextNode((task.deadline) ? parseInt((task.deadline - date) / 86400, 10) + ' Days left ' : ' - '));
      } else {
        DOMUtils.removeChildElements(div1);
        DOMUtils.removeChildElements(div2);
        
        div1.appendChild(document.createTextNode((PPublishUtils.object(task.state_id, states)) ? PPublishUtils.object(task.state_id, states).name : ''));
        div1.appendChild(document.createTextNode(', '));
        
        div2.appendChild(document.createTextNode((task.deadline) ? parseInt((task.deadline - date) / 86400, 10) + ' Days left ' : '- '));

        btn = document.createElement('input');
        btn.type = 'button';
        btn.className = 'button';
        btn.value = tmp.name;
        btn._state = r.s;
        btn._transition = tmp;
        btn._task = task;
        btn.onclick = function() { this.disabled = true;
                                   if (parseInt((this._task.deadline - date) / 86400, 10) < 0) {
                                     var popupBox = new ModulePopupBox();
                                     var div = document.createElement('div');
                                     var task = this._task;
                                     var transition = this._transition;
                                     var pos = DOMUtils.findPos(this);
                                     var func1 = function() { if (task._text == '') {
                                                                window.alert('不能为空');
                                                                return;
                                                              }
                                                              ModelUtils.op(transition, task, r, user, date, callbackFunc, null, pos, {overdue: task._text});
                                                              popupBox._close();
                                                            };
                                     var func2 = function() { popupBox._close();
                                                              callbackFunc();
                                                            };
                                     MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: 'OverDue Reason', width: 500, height: 50 });
                                     new ModuleDialogInput(document, div, null, null, user, date, { object: task, dfault: ''});
                                   } else {
                                     ModelUtils.op(this._transition, this._task, r, user, date, callbackFunc, null, DOMUtils.findPos(this), null);
                                   }
                                 };
        div3.appendChild(btn);
      }
    }
  }
};


ModelUtils.op = function(transition, task, result, user, date, callbackFunc, callbackFunc1, pos, options) {
  var e = ModelUtils.evt(task, result.s, transition, user, date);
  var t = ModelUtils.trk(task, transition);
  e.overdue = (options && options.overdue) ? options.overdue : '';
  
  if (callbackFunc1) {
    var callback = function() { callbackFunc(); callbackFunc1(); };
  } else {
    var callback = function() { callbackFunc(); };
  }
  
  var func = function() { task.state_id = transition.target_id;
                          task.deadline = parseInt(date, 10) + 86400 * transition.day;
                          new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [t], r: [] }], callback, { pos: pos });
                        };
  if (result.m.id == 3) {
    if (transition.name == 'Ask') {
      var popupBox = new ModulePopupBox();
      var div = document.createElement('div');
      
      var func1 = function() { if (task._text == '') {
                                 window.alert('CAN NOT BE EMPTY');
                                 return;
                               }
                               popupBox._close();
                               e.message = task._text;
                               func();
                             };
      var func2 = function() { popupBox._close();
                               callbackFunc();
                             };
      MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: 'Ask The Question', width: 500, height: 50 });
      new ModuleDialogInput(document, div, null, null, user, date, { object: task });
    } else if (transition.name == 'Answer') {
      var popupBox = new ModulePopupBox();
      var div = document.createElement('div');
      
      var func1 = function() { if (task._text == '') {
                                 window.alert('CAN NOT BE EMPTY');
                                 return;
                               }
                               popupBox._close();
                               e.message = task._text;
                               func();
                             };
      var func2 = function() { popupBox._close();
                               callbackFunc();
                             };
      MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: 'Answer The Question', width: 500, height: 50 });
      new ModuleDialogInput(document, div, null, null, user, date, {object: task});
    } else {
      func();
    }
  } else {
    if (transition.name == '备份') {
      task.is_asst_backup = 1;
      task.backuped_date = date;
      func();
    } else if (transition.name == '挑片') {
      task.is_asst_pickup = 1;
      task.chosen_date = date;
      func();
    } else if (transition.name == '评片') {
      var popupBox = new ModulePopupBox();
      var div = document.createElement('div');
      
      var func1 = function() { if (task.score == 0 && task.photographer_id != 0 ) {
                                 window.alert('请选择分数!');
                                 return;
                               }
                               if (task.score2 == 0 && task.photographer2_id != 0 ) {
                                 window.alert('请选择分数!');
                                 return;
                               }
                               popupBox._close();
                               e.message = 'score: ' + task.score + ((task.score2 != 0) ? '& ' + task.score : '') + ', ' + ((task.is_asst_retouched == 0) ? '不修片, ' : '修片, ') + 'detail: ' + ((task._text) ? task._text : '-');
                               task.state_id = (task.is_asst_retouched == 0) ? 4 : transition.target_id;
                               task.deadline = parseInt(date, 10) + 86400 * transition.day;
                               new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [t], r: [] }], callback, { pos: pos });
                             };
      var func2 = function() { popupBox._close();
                               callbackFunc();
                             };
      MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: '评片', width: 500, height: 50 });
      new ModuleDialogMark(document, div, null, null, user, date, { object: task, dfault: task.score_detail});
    } else if (transition.name == '评审') {
      var popupBox = new ModulePopupBox();
      var div = document.createElement('div');
      
      var func1 = function() { popupBox._close();
                               task.is_admin_checked = (task.is_admin_retouched == 0) ? 0 : 1;
                               task.checked_date = (task.is_admin_retouched == 0) ? 0 : date;
                               if (task.designer_id != 0) {
                                 task.is_choose_designer = (task.is_admin_retouched == 0) ? 0 : 1;
                                 task.choosedesigner_date = (task.is_admin_retouched == 0) ? 0 : date;
                                 task.state_id = (task.is_admin_retouched == 0) ? 7 : 9;
                                 e.message = ((task.is_admin_retouched == 0) ? '不修片 ' : '修片 ') + ((task.designer) ? ' , designer: ' + task.designer : '') + ((task._text) ? ' , detail: ' + task._text : '-');
                               } else {
                                 task.state_id = (task.is_admin_retouched == 0) ? 7 : transition.target_id;
                                 e.message = ((task.is_admin_retouched == 0) ? '不修片' : '修片') + ((task._text) ? ' , detail: ' + task._text : '');
                               }
                               task.deadline = parseInt(date, 10) + 86400 * transition.day;
                               new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [t], r: [] }], callback, { pos: pos });
                             };
      var func2 = function() { popupBox._close();
                               callbackFunc();
                             };
      MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: '评审', width: 500, height: 50 });
      new ModuleDialogCheck(document, div, null, null, user, date, { object: task, dfault: ''});
    } else if (transition.name == '挑选设计师') {
      var popupBox = new ModulePopupBox();
      var div = document.createElement('div');
      
      var func1 = function() { popupBox._close();
                               if (task.designer_id != 0) {
                                 e.message = task.designer;
                                 task.is_choose_designer = 1;
                                 task.choosedesigner_date = date;
                                 func();
                               } else {
                                 callbackFunc();
                               }
                             };
      var func2 = function() { popupBox._close();
                               task.designer_id = t.designer_id;
                               task.designer = t.designer;
                               callbackFunc();
                             };
      MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: 'Pick a designer', width: 320, height: 50 });
      new ModuleDialogPickDesigner(document, div, null, null, user, date, { object: task });
    } else if (transition.name == '上传') {
      task.is_des_retouched = 1;
      task.retouched_date = date;
      func();
    } else if (transition.name == '完成排序') {
      var popupBox = new ModulePopupBox();
      var div = document.createElement('div');
      
      var func1 = function() { if (task.ub_akl == 0 && task.ub_mel == 0 && task.ub_syd == 0 && task.ub_nyc == 0 && task.ub_bne == 0 && task.ub_xtrao == 0) {
                                 window.alert('至少选择一个用片公司');
                                 return;
                               }
                               popupBox._close();
                               e.message = '发片计划时间: ' + SimpleDate.format(SimpleDate.fromStamp(task.prearrange_date)) + ', 用片公司: (' + PPublishUtils.branch(task) + ')';
                               task.state_id = transition.target_id;
                               task.deadline = parseInt(date, 10) + 86400 * transition.day;
                               task.is_admin_sorted = 1;
                               task.sorted_date = date;
                               var note = pz_task_note.instance();
                               note.task_id = task.id;
                               note.note_user_id = user.id;
                               note.note_date = date;
                               note.note_content = e.message;
                               note.is_planning = 1;
                               new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [t], r: [] }, { n: 'pz_task_note', a: [note], r: [] }], callback, { pos: pos });
                             };
      var func2 = function() { popupBox._close();
                               callbackFunc();
                             };
      MiscUtils.dialog(popupBox, pos, div, func1, func2, { t: '片子排序', width: 500, height: 50 });
      new ModuleDialogArrange(document, div, null, null, user, date, { object: task});
    } else if (transition.name == '安排') {
      task.is_asst_arranged = 1;
      task.authorarranged_date = date;
      func();
    } else if (transition.name == '收取') {
      task.is_asst_received = 1;
      task.authorreceived_date = date;
      func();
    } else if (transition.name == '发片') {
      task.is_asst_published = 1;
      task.published_date = date;
      func();
    } else if (transition.name == '确认') {
      task.is_completed = 1;
      task.is_confirm_published = 1;
      task.confirmpublished_date = date;
      func();
    } else {
      func();
    }
  }
};

ModelUtils.reverse = function(task, track, callbackFunc, pos, date, user, notes) {
  var id = task.id; var ns = [];
  task = MiscUtils.clone(track);
  task.id = id;
  
  var e = pz_task_event.instance();
  e.task_id = task.id;
  e.date = date;
  e.user_id = user.id;;
  e.state_id = track.state_id;
  e.transition_id = 0;
  e.message = 'REVERSE: ';
  
  if (task.state_id == 10) {
    new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [], r: [track] }, { n: 'pz_task_note', a: [], r: notes }], callbackFunc, { pos: pos });
  } else {
    new RequestUtils()._write2([{ n: 'pz_task', a: [task], r: [] }, { n: 'pz_task_event', a: [e], r: [] }, { n: 'pz_task_track', a: [], r: [track] }], callbackFunc, { pos: pos });
  }
};